﻿//Write a method that return the maximal element in a portion of array of integers starting at given index. Using it write another method that sorts an array in ascending / descending order.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReturnTheMaximalElementInPortion
{
    class ReturnTheMaximalElementInPortion
    {
        static int[] Sort(int[] arr, int[] sortedarr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                int[] newarr = new int[arr.Length - i];
                Array.Copy(arr, i, newarr, 0, arr.Length - i);
                int max = newarr.Max();
                if (Array.IndexOf(arr, max) < i) arr[Array.IndexOf(arr, max)] = 0;
                int buffer = arr[Array.IndexOf(arr, max)];
                arr[Array.IndexOf(arr, max)] = arr[i];
                arr[i] = buffer;
                sortedarr[i] = max;
            }
            return sortedarr;
        }

        static void PrintArr(int[] arr)
        {
            foreach (int number in arr)
            {
                Console.Write(number + " ");
            }
        }

        static void Main()
        {
            int[] arr = new int[] { 5, 4, 3, 1, 3, 3, 1, 2, 3, 6, 8, 5, 3, 1, 2 };
            Console.WriteLine("Enter starting index: ");
            long start = long.Parse(Console.ReadLine());
            int[] newarr = new int[arr.Length - start];
            Array.Copy(arr, start, newarr, 0, arr.Length - start);
            int maxelement = newarr.Max();
            Console.WriteLine(maxelement);
            int[] sortedarr = new int[arr.Length];
            PrintArr(Sort(arr, sortedarr));
        }
    }
}
